<?php


namespace App\Services;


use app\api\service\UserService;
use app\electric\service\MeterSettlementService;
use App\Models\ElectricElectricity;
use App\Models\ElectricMeterReadingCompute;
use App\Models\ElectricMeterReadingLog;
use App\Models\ElectricPaymentOrder;
use App\Models\ElectricWater;
use App\Models\Users;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class BillService extends BaseService
{
    public static $query;

    public function __construct()
    {
        Db::connection("mysql_center")->enableQueryLog();
        self::$query = DB::connection("mysql_center");
    }

    /**
     * @param $total_log
     * @param array $date
     * @return array
     */
    public static function get_month_cost($total_log, array $date): array
    {
        $ele_month = $water_month = 0;
        $ele_month_arr = array_filter($total_log, static function ($item) use ($date) {
            return $item['year'] == $date[0] && $item['type'] == 1;//筛选
        });
        if (!empty($ele_month_arr)) {
            $ele_month = $ele_month_arr[0]['month_cost'];
        }
        $water_month_arr = array_filter($total_log, static function ($item) use ($date) {
            return $item['year'] == $date[0] && $item['type'] == 2;
        });
        if (!empty($water_month_arr)) {
            $water_month = $water_month_arr[0]['month_cost'];
        }
        return array($ele_month, $water_month);
    }

    /**
     * @param $total_log
     * @param array $date
     * @return array
     */
    public static function get_total_cost($total_log, array $date): array
    {
        $ele_total = $water_total = 0;
        $ele_total_arr = array_filter($total_log, static function ($item) {
            return $item['type'] == 1 && $item['is_delete'] == 0;
        });
        if (!empty($ele_total_arr)) {
            $ele_total = $ele_total_arr[0]['total_cost'];
        }
        $water_total_arr = array_filter($total_log, static function ($item) {
            return $item['type'] == 2 && $item['is_delete'] == 0;
        });
        if (!empty($water_total_arr)) {
            $water_total = $water_total_arr[0]['total_cost'];
        }
        return array($ele_total, $water_total);
    }

    /**
     * 获取账单列表
     * @param $date
     * @param $code
     * @param $type
     * @return array
     */
    public function getBillList($date, $code, $type): array
    {

        $table = '';
        //根据表码去查表地址
        $table_address = self::$query
            ->table('electric_electricity')
            ->where(['id' => $code])
            ->value('tableAddress');
        $days = date('t', strtotime($date));
        $data_s = strtotime($date . '-01');
        $data_e = strtotime($date . '-' . $days . ' 23:59:59');
        $list = ElectricMeterReadingLog::where(['tableAddress' => $table_address, 'type' => $type])
            ->whereBetween("day_time", [$data_s, $data_e])
            ->orderByDesc('create_time')
            ->select(Db::raw("day_count,day_cost,day_time"))
            ->get();
        if ($list->count() > 0) {//判断数组是否为空
            $list = $list->each(function (&$item) {
                $item['month'] = date('m', $item['day_time']);
                $item['day'] = date('d', $item['day_time']);
            })->toArray();
            $total = array_sum(array_column($list, 'day_cost'));
        } else {
            $total = 0;
            $list = [];
        }
        $data['total'] = $total;
        $data['list'] = $list;
        return $data;
    }

    /**
     * 获取电费，水费【全年水电费，当前年份的水电费和本周水电费】
     * @param $param
     * @param array $owner
     * @return array
     */
    public function getDetails($param, $owner = []): array
    {
        $code = $param['code'];
        $type = $param['type'];

        //查询当前市值
        $db = [];
        if ($type == 1) {
            $db = self::$query->table("electric_electricity");
            $column_str = 'electric_id';
        } else {
            $db = self::$query->table('electric_water');
            $column_str = 'water_id';
        }
        $num = $owner['community'];
        //通过code查表地址
        $tableaddress = $db->where(['code' => $code])->value('tableAddress');
        $etable = 'electric_electricity_data' . $num;
        $ewhere['electricity_code'] = $tableaddress;
        $ewhere['community_id'] = $owner['community'];
        $ewhere['object_name'] = 'electricity';
        $electricity = self::$query->table($etable)->where($ewhere)->orderByDesc('create_time')->value('object_val');
        $table = '';
        //总用电量和总费用
        $data = ElectricMeterReadingCompute::where(['meter_code' => $tableaddress, 'type' => $type, 'is_deleted' => 0])
            ->select(Db::raw('total_cost,total_count,balance'))
            ->first();
        if ($data != null) {
            $data = $data->toArray();
        }
        if (empty($data)) {
            $data['total_cost'] = 0;
            $data['balance'] = 0;
        }
        $data['total_count'] = $electricity;
        //结算日期查询
        $time = ElectricMeterReadingLog::where(['type' => $type, 'user_id' => $owner['id'], 'meter_id' => $owner[$column_str]])
            ->orderByDesc('create_time')
            ->value('day_time');
        $data['settlement_time'] = $time ? date('Y-m-d', $time) : '---';
        //当前年份的数据统计
        $year = date('Y', time());
        $db = ElectricMeterReadingLog::where(['tableAddress' => $tableaddress, 'year' => $year, 'type' => $type])
            ->select(Db::raw("sum(day_count) as count, sum(day_cost) as cost"))
            ->get();
        if ($db->count() > 0) {
            $yearData = $db->toArray();
        } else {
            $yearData = $db->toArray();
        }
        //当前日期
        $sdefaultDate = date('Y-m-d');
        //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
        $first = 1;
        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
        $w = date('w', strtotime($sdefaultDate));
        //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
        $week_start = Carbon::now()->startOfWeek();
        //本周结束日期
        $week_end = Carbon::now()->endOfWeek();
        $dateData = [];
        for ($i = 0; $i < 7; $i++) {
            $dateData[$i]['date'] = date('Y-m-d', strtotime("$week_start +$i days"));
            $dateData[$i]['count'] = 0;
        }
        $db1 = ElectricMeterReadingLog::where(['tableAddress' => $tableaddress, 'type' => $type])
            ->whereBetween("day_time", [$week_start, $week_end])
            ->select(Db::raw('day_count, day_cost,day_time'))
            ->get();
        if ($db1->count() > 0) {
            $weekData = $db1->toArray();
        } else {
            $weekData = [];
        }
        $now = date('Y-m-d', time());
        foreach ($dateData as $k => $v) {
            //如果没有数据就只显示今天的实时数据
            if (empty($weekData)) {
                if ($v['date'] == $now) {
                    //如果是当前日期，就显示实时数据
                    $dateData[$k]['count'] = $electricity;
                }
            } else {
                foreach ($weekData as $k1 => $v1) {
                    $weekDataTime = date('Y-m-d', $v1['day_time']);
                    if ($v['date'] == $weekDataTime) {
                        $dateData[$k]['count'] = $v1['day_count'];
                    } elseif ($v['date'] == $now) {
                        //如果是当前日期，就显示实时数据
                        $dateData[$k]['count'] = $electricity;
                    }
                }
            }

        }
        $chartData = array_column($dateData, 'count');

        $data['electricity'] = $electricity;

        $return = [
            'total_data' => $data,
            'year_data' => $yearData,
            'total_electricity' => $electricity,
            'chart_data' => ['ydata' => $chartData, 'max' => max($chartData)]
        ];

        return $return;
    }

    public function getPropertyLog($year, $owner): array
    {
        $list = ElectricMeterReadingLog::where(['type' => 3, 'user_id' => $owner['id'], 'year' => $year])
            ->select(Db::raw("user_id,year,month,day_cost,create_time"))
            ->get();

        if ($list->count() > 0) {
            $list = $list->each(function ($item) {
                $item['username'] = Users::where(["id" => $item['username']])->value("username") ?: "神秘人";
                return $item;
            })->toArray();
            $total = array_sum(array_column($list, 'day_cost'));
        } else {
            $list = [];
            $total = 0;
        }

        return [
            'list' => $list,
            'total' => $total
        ];
    }


    public function getProperty($months, $owner)
    {
        $balance = $owner['property_bill_balance'];
        //当前绑定的物业费收费id
        $planId = $owner['property_plan_id'];
        $fee = self::$query->table('electric_charging_scheme')
            ->where(['id' => $planId])
            ->value('unit_price');
        $next_month = date('Y-m', strtotime('+1 months'));
        $list = [];
        $fee_ = $fee * $owner['housing_area'];
        $total = $balance + $fee_ * 6;
        for ($i = 0; $i < $months; $i++) {
            $list[$i]['date'] = date('Y-m', strtotime("$next_month +$i months"));
            $list[$i]['fee'] = $fee_;
            $total = bcsub($total, $fee_, 2);
            $list[$i]['balance'] = $total;
        }
        return [
            'balance' => $balance,
            'month_data' => $list
        ];
    }

    /**
     *缴费记录
     * @param $ownerId
     * @param $param
     * @return array
     * @author ypp
     */
    public function payBillList($ownerId, $param): array
    {
        $freeType = $param['feeType'];
        $limit = $param['limit'] ?: 10;
        $page = $param['page'] ?: 1;
        $datestr = explode('.', $param['date_time']);
        if ($freeType == 3) {//物业费
            $where = ['status' => 2, 'user_id' => $ownerId, 'year' => $datestr];
        } else {
            $year = $datestr[0];
            $month = (int)$datestr[1];
            $where = ['status' => 2, 'user_id' => $ownerId, 'year' => $year, 'month' => $month];
        }

        $model = ElectricPaymentOrder::where($where);
        if ($freeType) {
            $model->where(['feeType' => $freeType]);
        }
        $db2 = $model
            ->orderByDesc('id')
            ->offset($page)
            ->limit($limit)
            ->select(DB::raw("paymentMoney,create_time,feeType,user_id,id"))
            ->get();

        if ($db2->count() > 0) {
            $res = $db2->each(function (&$item) {
                $user = Users::select(DB::raw('username,id'))->find($item['user_id']);
                $item['name'] = $user['username'] ?: '神秘人';
            })->toArray();
        } else {
            $res = [];
        }
        $fee = array_column($res, 'paymentMoney');
        $total = array_sum($fee);
        return [
            'total' => $total,
            'list' => $res
        ];

    }

    /**
     * 账单   水费  电费 物业费
     * @param $ownerId
     * @return array
     */
    public static function billInfo($ownerId): array
    {
        //查询电费/水费核算表的总消费
        $db = self::$query->table('electric_meter_reading_compute')
            ->where(['user_id' => $ownerId, 'is_deleted' => 0])
            ->select('total_cost,type')
            ->get();
        if ($db->count() > 0) {
            $Cost = $db->toArray();
        } else {
            $Cost = [];
        }
        $eleCost = $waterCost = $proCost = 0;
        foreach ($Cost as $k => $v) {
            if ($v['type'] == 1) {
                $eleCost = $v['total_cost'];
            } elseif ($v['type'] == 2) {
                $waterCost = $v['total_cost'];
            }
        }
        /*//查询水费核算表的总消费
        $waterCost =self::$query->table('electric_water_reading_compute')
            ->where(['user_id'=>$ownerId])
            ->value('total_cost');*/
        //查询物业费总消费
        $proCost = self::$query->table('electric_meter_reading_log')
            ->where(['user_id' => $ownerId, 'type' => 3])
            ->sum('day_cost');
        return [
            'total' => bcadd($eleCost, bcadd($waterCost, $proCost, 2), 2),
            'electric_cost' => $eleCost ?: 0,
            'water_cost' => $waterCost ?: 0,
            'pro_cost' => $proCost ?: 0
        ];
    }

    /**
     * 获取缴费页面的信息
     * @author ypp
     */
    public static function getBillDetail($owner)
    {
        $user_id = $owner['id'];
        $date = explode('-', date('Y-m', time()));
        //水电 本月消费
        $ele_month = $water_month = 0;
        $db = ElectricMeterReadingLog::selectRaw(Db::raw('sum(day_cost) as month_cost,type'))
            ->where('type', 'in', [1, 2])
            ->where(['year' => $date[0], 'month' => $date[1], 'user_id' => $user_id])
            ->groupBy('type')
            ->get();
        if ($db->count()) {
            $monthCost = $db->toArray();

            $ele_arr = array_filter($monthCost, function ($item) {
                return $item['type'] == 1;
            });
            $ele_month = $ele_arr[0]['month_cost'];

            $water_arr = array_filter($monthCost, function ($item) {
                return $item['type'] == 2;
            });
            $water_month = $water_arr[0]['month_cost'];
        }


        //水电累计消费
        $ele_total = $water_total = 0;
        $db1 = ElectricMeterReadingCompute::selectRaw(Db::raw('total_cost,type'))
            ->where(['user_id' => $user_id, 'is_deleted' => 0])
            ->groupBy('type')
            ->get();
        if ($db1->count()) {
            $totalCost = $db->toArray();
            $ele_arr1 = array_filter($totalCost, function ($item) {
                return $item['type'] == 1;
            });
            $ele_total = $ele_arr1[0]['total_cost'];

            $water_arr1 = array_filter($totalCost, function ($item) {
                return $item['type'] == 2;
            });
            $water_total = $water_arr1[0]['total_cost'];
        }
        //物业费累计消费
        $property = ElectricMeterReadingLog::where(['user_id' => $user_id, 'type' => 3])
            ->sum('day_cost');
        return [
            'electricity_bill_balance' => $owner['electricity_bill_balance'],
            'water_bill_balance' => $owner['water_bill_balance'],
            'property_bill_balance' => $owner['property_bill_balance'],
            'electric_month' => $ele_month,
            'water_month' => $water_month,
            'electric_total' => $ele_total,
            'water_total' => $water_total,
            'property_total' => $property
        ];
    }

    /**
     * 能耗分析 月份
     * @param $param
     * @param $owner
     * @return array
     */
    public function getMonthEnergyAnalysis($param, $owner): array
    {
        $type = $param['type'];
        $year = $param['year'] ?? date('Y', time());
        $yData = [];
        $xData = [];
        $max = 0;
        $arr = [];
        $nowYear = date('Y', time());
        $data = [];
        $lent = 0;
        if ($year == $nowYear) {
            $datestr = date('m', time());
            $lent = (int)$datestr;
            for ($i = 0; $i < $lent; $i++) {
                $data[$i]['day_time'] = date('Y-m', strtotime('-' . $i . ' month'));
                $data[$i]['count'] = 0;
                $data[$i]['cost'] = '0.00';
            }
        } else {
            $lent = 12;
            for ($i = 0; $i < 12; $i++) {
                $data[$i]['day_time'] = $year . '-' . (12 - $i);
                $data[$i]['count'] = 0;
                $data[$i]['cost'] = '0.00';
            }
        }
        if ($type == 1) {
            //$table = 'electric_meter_reading_log';
            $chargeTable = 'electric_electricity';
            $charge = ElectricElectricity::where(['code' => $param['code']])->first();
            $mete_id = $owner['electric_id'];
        } elseif ($type == 2) {
            // $table = 'electric_water_reading_log';
            $charge = ElectricWater::where(['code' => $param['code']])->first();
            $mete_id = $owner['water_id'];
        }
        $total_count = 0;
        $total_cost = 0;
        $start_time = strtotime($data[$lent - 1]['day_time']);
        $end_time = strtotime($data[0]['day_time']);
        $db = ElectricMeterReadingLog::selectRaw(Db::raw('sum(day_count) as count,month_time,sum(day_cost) as cost'))
            ->where(['meter_id' => $mete_id, 'type' => $type])
            ->whereBetween("month_time", [$start_time, $end_time])
            ->groupBy('month_time')
            ->get();
        if ($db->count()) {
            $list = $db->toArray();
            foreach ($data as $k => $v) {
                foreach ($list as $key => $val) {
                    $list[$key]['day_time'] = date('Y-m', $val['month_time']);
                    if ($v['day_time'] == date('Y-m', $val['month_time'])) {
                        $data[$k]['count'] = $val['count'];
                        $data[$k]['cost'] = $val['cost'];
                    }
                }
                $xData[] = ($k + 1) . '月';
                $yData[] = $data[$k]['count'];
            }
            $total_count = array_sum(array_column($list, 'count'));
            $total_cost = array_sum(array_column($list, 'cost'));
            $yData = array_reverse($yData);
            $max = max($yData);
            //查询收费方案

            if ($charge) {
                $charge = $charge->toArray();
                $priceArr = ['price1' => 0, 'degree1' => 0, 'price2' => 0, 'degree2' => 0, 'price3' => 0, 'degree3' => 0];
                if ($charge['step_price']) {  //阶梯价
                    $stepPrice = json_decode($charge['step_price'], 1);
                    $priceArr = $this->getPrice($stepPrice, $total_count);
                }
                if ($total_count != 0) {
                    $arr = [0 => (round($priceArr['degree1'] / $total_count, 2)) * 100,
                        (round($priceArr['degree2'] / $total_count, 2)) * 100,
                        (round($priceArr['degree3'] / $total_count, 2)) * 100];
                } else {
                    $arr = [0, 0, 0];
                }
            }
        }

        return [
            'xData' => $xData,
            'yData' => $yData,
            'max' => $max,
            'percent' => $arr,
            'total_count' => $total_count,
            'total_cost' => $total_cost,
            'list' => array_reverse($data),
        ];

    }

    /**
     * 获取价格
     * @param $stepPrice
     * @param $mult
     * @param $degree
     * @return float|int
     */
    public function getPrice($stepPrice, $degree)
    {

        $arr = ['price1' => 0, 'degree1' => 0, 'price2' => 0, 'degree2' => 0, 'price3' => 0, 'degree3' => 0];
        foreach ($stepPrice as $k1 => $v1) {
            switch ($k1) {
                case 0:
                    $price0 = $v1['unit_price'];
                    $min0 = $v1['minimum_amount'];
                    break;
                case 1:
                    $price1 = $v1['unit_price'];
                    $min1 = $v1['minimum_amount'];
                    break;
                case 2:
                    $price2 = $v1['unit_price'];
                    $min2 = $v1['minimum_amount'];
                    break;
            }

            if ($k1 == 0 && $v1['minimum_amount'] < $degree && $degree <= $v1['maximum_amount']) {
                //$price = $v['object_val']*$mult*$v1['unit_price'];
                $arr['price1'] = $v1['unit_price'];
                $arr['degree1'] = $degree;
            } else {
                if ($k1 == 1 && $v1['minimum_amount'] < $degree && $degree <= $v1['maximum_amount']) { //用电量在第二阶梯
                    // $price = $v1['minimum_amount']*$mult*$price0 + ($v['object_val']-$v1['minimum_amount'])*$mult*$price1;
                    $arr['price1'] = $price0;
                    $arr['degree1'] = $v1['minimum_amount'];

                    $arr['price2'] = $price1;
                    $arr['degree2'] = $degree - $v1['minimum_amount'];

                } elseif ($k1 == 2 && $v1['minimum_amount'] < $degree) { //用电量在第三阶梯
                    //$price = ($v['object_val']-$v1['minimum_amount'])*$mult*$price2 +($v1['minimum_amount']-$min2)*$mult*$price1 + $min2*$mult*$price0;
                    $arr['price1'] = $price0;
                    $arr['degree1'] = $min1;

                    $arr['price2'] = $price1;
                    $arr['degree2'] = $min2 - $min1;

                    $arr['price3'] = $price2;
                    $arr['degree3'] = $degree - $v1['minimum_amount'];
                }
            }

        }
        return $arr;
    }

    /**
     * 能耗分析 日用电量
     * @param $param
     * @param $owner
     * @return array
     */
    public function getWeekEnergyAnalysis($param, $owner)
    {
        $type = $param['type'];
        $data = [];
        //取今天往前7天的数据
        $today_7 = date('Y-m-d', strtotime('-8 days'));
        $today = date('Y-m-d', strtotime('-1 days'));

        for ($i = 0; $i < 7; $i++) {
            $data[$i]['day_time'] = date('Y-m-d', strtotime('-' . ($i + 1) . ' days'));
            $data[$i]['count'] = '0.00';
            $data[$i]['cost'] = '0.00';
        }

        if ($type == 1) {
            $mete_id = $owner['electric_id'];
        } elseif ($type == 2) {
            $mete_id = $owner['water_id'];
        }
        $total_count = $total_cost = 0;
        $xData = [];
        $yData = [];
        $max = 0;
        $db = ElectricMeterReadingLog::selectRaw(DB::raw('day_count,day_time,day_cost'))
            ->where(['meter_id' => $mete_id, 'type' => $type])
            ->whereBetween("day_time", [strtotime($today_7), strtotime($today)])
            ->groupBy('day_time')
            ->get();
        if ($db->count()) {
            $list = $db->toArray();
            foreach ($data as $k => $v) {
                $data[$k]['day_str'] = date('m-d', strtotime($v['day_time']));
                foreach ($list as $key => $val) {
                    $list[$key]['day_str'] = date('m-d', $val['day_time']);
                    if ($v['day_time'] == date('Y-m-d', $val['day_time'])) {
                        $data[$k]['count'] = $val['day_count'];
                        $data[$k]['cost'] = $val['day_cost'];
//
                    }

                }
                $weekStr = self::getWeek(strtotime($v['day_time']));
                $xData[] = $weekStr;
                $yData[] = $data[$k]['count'];
            }
            $xData = array_reverse($xData);
            $yData = array_reverse($yData);
            $max = max($yData);
            $total_count = array_sum(array_column($list, 'day_count'));
            $total_cost = array_sum(array_column($list, 'day_cost'));
        }
        return [
            'xData' => $xData,
            'yData' => $yData,
            'max' => $max,
            'total_count' => $total_count,
            'total_cost' => $total_cost,
            'list' => array_reverse($data),
        ];

    }

    /**
     * 能耗分析 年用电量
     * @param $param
     * @param $owner
     * @return array
     */
    public function getYearEnergyAnalysis($param, $owner)
    {
        $type = $param['type'];
        $data = [];
        //取今天往前7年的数据
        $today_7 = date('Y', strtotime('-8 year'));
        $today = date('Y', strtotime('-1 year'));
        for ($i = 0; $i < 7; $i++) {
            $data[$i]['day_time'] = date('Y', strtotime('-' . ($i + 1) . ' year'));
            $data[$i]['count'] = '0.00';
            $data[$i]['cost'] = '0.00';
        }
        $meter_id = 0;
        switch ($type) {
            case 1:
                $meter_id = $owner['electric_id'];
                break;
            case 2:
                $meter_id = $owner['water_id'];
                break;
            default:
                break;
        }
        $total_count = $total_cost = 0;
        $xData = [];
        $yData = [];
        $max = 0;
        $start_time = strtotime(date("{$today_7}-01-01 00:00:00"));
        $end_time = strtotime(date("{$today}-12-31 23:59:59"));
        $db = ElectricMeterReadingLog::selectRaw(DB::raw('day_count,day_time,day_cost,year'))
            ->where(['meter_id' => $meter_id, 'type' => $type])
            ->whereBetween('day_time', [$start_time, $end_time])
            ->groupBy('day_time')->get();

        if ($db->count()) {
            $list = $db->toArray();
            $year_arr = [];
            foreach ($data as $k => $v) {
                $data[$k]['day_str'] = date('m-d', strtotime($v['day_time']));
                $start_time = strtotime(date("{$v['day_time']}-01-01 00:00:00"));
                $end_time = strtotime(date("{$v['day_time']}-12-31 23:59:59"));
                $arr = ElectricMeterReadingLog::selectRaw(DB::raw('day_count,day_time,day_cost,year'))
                    ->where(['meter_id' => $meter_id, 'type' => $type])
                    ->whereBetween('day_time', [$start_time, $end_time])
                    ->get();

                if ($arr->count()) {
                    $arr = $arr->toArray();
                    $count = round(array_sum(array_column($arr, "day_count")),2);
                    $cost = round(array_sum(array_column($arr, "day_cost")),2);
                    $yData[] = $count;
                } else {
                    $count = 0;
                    $cost=0;
                }
                $data[$k]['count']=$count;
                $data[$k]['cost']=$cost;
                $yData[] = $count;
                $xData[] = $v['day_time'];

            }
            $xData = array_reverse($xData);
            $yData = array_reverse($yData);
            $max = max($yData);
            $total_count = round(array_sum(array_column($list, 'day_count')),2);
            $total_cost = round(array_sum(array_column($list, 'day_cost')),2);
        }
        return [
            'xData' => $xData,
            'yData' => $yData,
            'max' => $max,
            'total_count' => $total_count,
            'total_cost' => $total_cost,
            'list' => array_reverse($data),
        ];

    }

    /**
     * 根据时间返回周几
     * @param $date
     * @return string
     */
    public static function getWeek($date)
    {
        $w = date('w', $date);
        $str = '';
        switch ($w) {
            case 0:
                $str = '周天';
                break;
            case 1:
                $str = '周一';
                break;
            case 2:
                $str = '周二';
                break;
            case 3:
                $str = '周三';
                break;
            case 4:
                $str = '周四';
                break;
            case 5:
                $str = '周五';
                break;
            case 6:
                $str = '周六';
                break;
        }
        return $str;
    }
}
